package dict

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"
	"fast-boot/common/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// Update
func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateLogic) Update(in *types.SysDictForm) (resp *types.SysDictFormResp, err error) {
	info := model.SysDictModel{}
	l.svcCtx.GormClient.GormDb.First(&info, in.Id)

	if info.Id == 0 {
		logx.WithContext(l.ctx).Errorf("不存在: %s", in.Id)
		return nil, xerr.NewErrMsg("不存在")
	}
	info.Code = in.Code
	info.Name = in.Name
	info.Remark = in.Remark
	info.Status = int8(in.Status)

	l.svcCtx.GormClient.GormDb.Save(&info)

	return &types.SysDictFormResp{}, nil
}
